package com.yida.wy.config.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.wy.config.domain.WyPolicyNotice;
import com.yida.wy.config.domain.dto.WyPolicyNoticeDetailDTO;
import com.yida.wy.config.domain.request.WyPolicyNoticeChangeStatusRequest;
import com.yida.wy.config.domain.request.WyPolicyNoticeEditRequest;
import com.yida.wy.config.domain.request.WyPolicyNoticeSaveRequest;
import com.yida.wy.config.mapper.WyPolicyNoticeMapper;
import com.yida.wy.config.service.IWyPolicyNoticeService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * 公维金政策公告Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-30
 */
@Service
public class WyPolicyNoticeServiceImpl implements IWyPolicyNoticeService {
    @Autowired
    private WyPolicyNoticeMapper gwjPolicyNoticeMapper;

    /**
     * 查询公维金政策公告
     * 
     * @param id 公维金政策公告主键
     * @return 公维金政策公告
     */
    @Override
    public WyPolicyNotice selectGwjPolicyNoticeById(Long id) {
        return gwjPolicyNoticeMapper.selectGwjPolicyNoticeById(id);
    }

    /**
     * 查询公维金政策公告列表
     * 
     * @param gwjPolicyNotice 公维金政策公告
     * @return 公维金政策公告
     */
    @Override
    public List<WyPolicyNotice> selectGwjPolicyNoticeList(WyPolicyNotice gwjPolicyNotice) {
        return gwjPolicyNoticeMapper.selectGwjPolicyNoticeList(gwjPolicyNotice);
    }

    /**
     * 新增公维金政策公告
     * 
     * @param gwjPolicyNotice 公维金政策公告
     * @return 结果
     */
    @Override
    public int insertGwjPolicyNotice(WyPolicyNotice gwjPolicyNotice) {
        gwjPolicyNotice.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        gwjPolicyNotice.setCreateTime(DateUtils.getNowDate());
        return gwjPolicyNoticeMapper.insertGwjPolicyNotice(gwjPolicyNotice);
    }

    /**
     * 修改公维金政策公告
     * 
     * @param gwjPolicyNotice 公维金政策公告
     * @return 结果
     */
    @Override
    public int updateGwjPolicyNotice(WyPolicyNotice gwjPolicyNotice) {
        gwjPolicyNotice.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        gwjPolicyNotice.setUpdateTime(DateUtils.getNowDate());
        return gwjPolicyNoticeMapper.updateGwjPolicyNotice(gwjPolicyNotice);
    }

    /**
     * 批量删除公维金政策公告
     * 
     * @param ids 需要删除的公维金政策公告主键
     * @return 结果
     */
    @Override
    public int deleteGwjPolicyNoticeByIds(Long[] ids) {
        return gwjPolicyNoticeMapper.deleteGwjPolicyNoticeByIds(ids);
    }

    /**
     * 删除公维金政策公告信息
     * 
     * @param id 公维金政策公告主键
     * @return 结果
     */
    @Override
    public int deleteGwjPolicyNoticeById(Long id) {
        return gwjPolicyNoticeMapper.deleteGwjPolicyNoticeById(id);
    }

    /**
     * 新增公维金政策公告
     * 
     * @param saveRequest 保存参数
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveGwjPolicyNotice(WyPolicyNoticeSaveRequest saveRequest) {
        // 拷贝参数
        WyPolicyNotice gwjPolicyNotice = new WyPolicyNotice();
        BeanUtil.copyProperties(saveRequest, gwjPolicyNotice);
        gwjPolicyNotice.setPolicyCode(generatePolicyCode());
        // 保存公告
        if (1 != this.insertGwjPolicyNotice(gwjPolicyNotice)) {
            throw new ServiceException("政策公告保存失败");
        }
        return Boolean.TRUE;
    }

    /**
     * 修改公维金政策公告
     * 
     * @param editRequest 修改参数
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editGwjPolicyNotice(WyPolicyNoticeEditRequest editRequest) {
        // 查询政策公告
        WyPolicyNotice gwjPolicyNotice = gwjPolicyNoticeMapper.selectGwjPolicyNoticeById(editRequest.getId());
        if (ObjectUtil.isEmpty(gwjPolicyNotice)) {
            throw new ServiceException("未查询到对应政策公告");
        }
        // 拷贝参数
        BeanUtil.copyProperties(editRequest, gwjPolicyNotice);
        // 修改公告
        if (1 != this.updateGwjPolicyNotice(gwjPolicyNotice)) {
            throw new ServiceException("政策公告修改失败");
        }
        return Boolean.TRUE;
    }

    /**
     * 查询公维金政策公告详情
     * 
     * @param id 主键
     * @return
     */
    @Override
    public WyPolicyNoticeDetailDTO getDetail(Long id) {
        // 查询政策公告
        WyPolicyNotice gwjPolicyNotice = gwjPolicyNoticeMapper.selectGwjPolicyNoticeById(id);
        if (ObjectUtil.isEmpty(gwjPolicyNotice)) {
            throw new ServiceException("未查询到对应政策公告");
        }
        // 拷贝参数
        WyPolicyNoticeDetailDTO detailDTO = new WyPolicyNoticeDetailDTO();
        BeanUtil.copyProperties(gwjPolicyNotice, detailDTO);
        return detailDTO;
    }

    /**
     * 修改发布状态
     * 
     * @param changeStatusRequest 发布状态修改参数
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeStatus(WyPolicyNoticeChangeStatusRequest changeStatusRequest) {
        // 查询政策公告
        WyPolicyNotice gwjPolicyNotice = gwjPolicyNoticeMapper.selectGwjPolicyNoticeById(changeStatusRequest.getId());
        if (ObjectUtil.isEmpty(gwjPolicyNotice)) {
            throw new ServiceException("未查询到对应政策公告");
        }
        if (changeStatusRequest.getStatus().equals(gwjPolicyNotice.getStatus())) {
            throw new ServiceException("请勿重复修改相同发布状态");
        }
        // 修改发布状态
        gwjPolicyNotice.setStatus(changeStatusRequest.getStatus());
        // 更新政策公告
        if (1 != this.updateGwjPolicyNotice(gwjPolicyNotice)) {
            throw new ServiceException("发布状态修改失败");
        }
        return Boolean.TRUE;
    }

    /**
     * 生成正常编号
     * 
     * @return
     */
    public String generatePolicyCode() {
        String policyCode = "";
        String date = DateUtils.dateTimeNow(DateUtils.YYYYMMDD);
        WyPolicyNotice gwjPolicyNotice = new WyPolicyNotice();
        gwjPolicyNotice.setPolicyCode(date);
        List<WyPolicyNotice> gwjPolicyNotices = gwjPolicyNoticeMapper.selectGwjPolicyNoticeList(gwjPolicyNotice);
        int size = gwjPolicyNotices.size();
        if (0 == size) {
            policyCode = date + "1";
        } else {
            policyCode = date + String.valueOf(size + 1);
        }
        return policyCode;
    }
}
